import 'package:aichat_flutter_wallet/module/module_dapp/widgets/hover_status.dart';
import 'package:aichat_flutter_wallet/module_common/provider/wallet_info_provider.dart';
import 'package:aichat_flutter_wallet/module_common/utils/colors.dart';
import 'package:aichat_flutter_wallet/module_common/widgets/dialog/bottom_sheet_widget.dart';
import 'package:aichat_flutter_wallet/module_common/widgets/home_bar/home_bar.dart';
import 'package:aichat_flutter_wallet/module_common/widgets/public_tip/public_tip.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:lib_database/lib_database.dart';
import 'package:lib_util/lib_util.dart';

class SetWalletName extends StatefulWidget {
  String text = '';

  SetWalletName({super.key, required this.text});

  @override
  State<StatefulWidget> createState() {
    return _SetWalletNameState();
  }
}

class _SetWalletNameState extends State<SetWalletName> {
  TextEditingController controller = TextEditingController();
  FocusNode focusNode = FocusNode();

  @override
  void initState() {
    controller.text = widget.text;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return BottomSheetWidget(
      title: 'Change Wallet Name',
      expand: false,
      child: Container(
        padding: EdgeInsets.fromLTRB(20.w, 0, 20.w, 0),
        child: Column(
          children: [
            SizedBox(
              height: 10.w,
            ),
            Container(
              height: 46.w,
              padding: EdgeInsets.symmetric(horizontal: 15.w),
              decoration: BoxDecoration(
                  color: AppColors.c_F6F6F6,
                  borderRadius: BorderRadius.all(Radius.circular(10))),
              child: Row(
                children: [
                  Expanded(
                    child: TextField(
                      inputFormatters: [
                        LengthLimitingTextInputFormatter(20),
                        FilteringTextInputFormatter.allow(
                            RegExp("[a-zA-Z0-9 ]")),
                        // 限制输入长度为20个字符
                      ],
                      keyboardType: TextInputType.text,
                      controller: controller,
                      focusNode: focusNode,
                      onTapOutside: (PointDownEvent) {
                        focusNode.unfocus();
                      },
                      onEditingComplete: () {
                        focusNode.unfocus();
                      },
                      onChanged: (val) {
                        setState(() {});
                      },
                      cursorColor: Color.fromRGBO(134, 103, 254, 1),
                      style: TextStyle(
                        fontSize: 15.sp,
                        height: 20.w / 15.w,
                        color: Color.fromRGBO(51, 51, 51, 1),
                      ),
                      decoration: InputDecoration(
                        border: InputBorder.none,
                        hintText: 'MPC Wallet A',
                        hintStyle: TextStyle(
                          fontSize: 14.sp,
                          height: 20.w / 14.w,
                          color: Color.fromRGBO(190, 189, 202, 1),
                        ),
                      ),
                      minLines: null,
                      maxLines: null,
                    ),
                  ),
                  Container(
                    height: 46.w,
                    alignment: Alignment.center,
                    child: Row(
                      children: [
                        Visibility(
                            visible: controller.text.length > 0,
                            child: GestureDetector(
                              onTap: () {
                                setState(() {
                                  controller.text = '';
                                });
                              },
                              child: Image.asset(
                                'assets/images/common/clear-input.png',
                                width: 16.w,
                                height: 16.w,
                              ),
                            )),
                        SizedBox(
                          width: 10.w,
                        ),
                        Text(
                          '${controller.text.length}',
                          style: TextStyle(
                              fontSize: 12.sp,
                              fontWeight: FontWeight.w400,
                              color: AppColors.c_333333),
                        ),
                        Text(
                          '/20',
                          style: TextStyle(
                              fontSize: 12.sp,
                              fontWeight: FontWeight.w400,
                              color: AppColors.c_BEBDCA),
                        )
                      ],
                    ),
                  )
                ],
              ),
            ),
            SizedBox(
              height: 20.w,
            ),
            HoverStatus(
              defaultColor: Color.fromRGBO(134, 103, 254, 1),
              hoverColor: (controller.text.isEmpty || controller.text.isEmpty)
                  ? Color.fromRGBO(134, 103, 254, 1)
                  : Color.fromRGBO(109, 77, 231, 1),
              radius: 10.w,
              child: Container(
                height: 46.w,
                alignment: Alignment.center,
                child: Text(
                  'Confirm',
                  style: TextStyle(
                    fontSize: 14.sp,
                    fontWeight: FontWeight.w500,
                    color: (controller.text.isEmpty || controller.text.isEmpty)
                        ? Color.fromRGBO(255, 255, 255, 0.5)
                        : Color.fromRGBO(255, 255, 255, 1),
                  ),
                ),
              ),
              onPressed: () async {
                String text = controller.text.trim();
                text = text.replaceAll(RegExp(r'^\s+|\s+$'), '');
                if (text.isEmpty) return;
                Wallet? wallet = context.read<WalletInfoNotifier>().wallet;
                if (wallet != null && text.isNotEmpty) {
                  context
                      .read<WalletInfoNotifier>()
                      .updateWalletName(wallet, text);
                  // //完成逻辑
                  Toast.show(context,
                          message: 'Operating succeeded',
                          duration: Duration(seconds: 2))
                      .then((_) {
                    Navigator.of(context).pop();
                  });
                } else {
                  Toast.show(context,
                      message: 'Operation failed',
                      status: '2',
                      duration: Duration(seconds: 2));
                }
              },
            ),
            HomeBar()
          ],
        ),
      ),
    );
  }
}
